<template>
  <div class="">
    <el-row :gutter="10"
      class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading"
      :data="dataList">
      <el-table-column label="爻序"
        align="center"
        prop="yaoSort"
        min-width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yj_yao_sort"
            :value="scope.row.yaoSort" />
        </template>
      </el-table-column>
      <el-table-column label="爻类别"
        align="center"
        prop="yaoSex"
        :show-overflow-tooltip="true"
        min-width="160">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yj_yao_sex"
            :value="scope.row.yaoSex" />
        </template>
      </el-table-column>
      <el-table-column label="吉凶"
        align="center"
        prop="jiXiong"
        :show-overflow-tooltip="true"
        min-width="160">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yj_ji_xiong"
            :value="scope.row.jiXiong" />
        </template>
      </el-table-column>
      <el-table-column label="爻词"
        align="center"
        prop="ci"
        :show-overflow-tooltip="true"
        min-width="160" />
      <el-table-column label="爻词解释"
        align="center"
        prop="ciDesc"
        :show-overflow-tooltip="true"
        min-width="160" />
      <el-table-column label="象词"
        align="center"
        prop="xiang"
        :show-overflow-tooltip="true"
        min-width="160" />
      <el-table-column label="象词解释"
        align="center"
        prop="xiangDesc"
        :show-overflow-tooltip="true"
        min-width="160" />

      <el-table-column label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        min-width="140">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" /> -->

    <!-- 添加或修改角色配置对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="80px">

        <el-form-item label="爻序"
          prop="yaoSort">
          <el-select v-model="form.yaoSort"
            placeholder="请选择爻序"
            clearable
            style="width: 100%">
            <el-option v-for="dict in dict.type.yj_yao_sort"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="爻类别"
          prop="yaoSex">
          <el-select v-model="form.yaoSex"
            placeholder="请选择爻类别"
            clearable
            style="width: 100%">
            <el-option v-for="dict in dict.type.yj_yao_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="吉凶"
          prop="jiXiong">
          <el-select v-model="form.jiXiong"
            placeholder="请选择级别"
            clearable
            style="width: 100%">
            <el-option v-for="dict in dict.type.yj_ji_xiong"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="爻词"
          prop="ci">
          <el-input type="textarea"
            v-model="form.ci"
            maxlength="2000"
            style="width: 100%"
            placeholder="请输入卦词">
          </el-input>
        </el-form-item>
        <el-form-item label="爻词解释"
          prop="ciDesc">
          <el-input type="textarea"
            v-model="form.ciDesc"
            maxlength="2000"
            style="width: 100%"
            placeholder="请输入爻词解释">
          </el-input>
        </el-form-item>
        <el-form-item label="象词"
          prop="xiang">
          <el-input type="textarea"
            v-model="form.xiang"
            maxlength="2000"
            style="width: 100%"
            placeholder="请输入象词">
          </el-input>
        </el-form-item>
        <el-form-item label="象词解释"
          prop="xiangDesc">
          <el-input type="textarea"
            v-model="form.xiangDesc"
            maxlength="2000"
            style="width: 100%"
            placeholder="请输入象词解释">
          </el-input>
        </el-form-item>
        <el-form-item label="备注"
          prop="remark"
          style="width: 100%;">
          <el-input type="textarea"
            v-model="form.remark"
            style="width: 100%;"
            maxlength="2000"
            placeholder="请输入备注">
          </el-input>
        </el-form-item>

      </el-form>
      <div slot="footer"
        class="dialog-footer">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  getYaoList,
  yaoDetail,
  addYao,
  updateYao,
  delYao,
} from "@/api/divination/divination64.js";
import { onInput } from '@/utils/util.js'

export default {
  name: "SayGua",
  dicts: ["yj_yao_sort", 'yj_yao_sex', 'yj_ji_xiong'],
  data () {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        guaId: null
      },
      // 数据列表
      dataList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  props: {
    guaId: {
      type: [String, Number]
    }
  },
  created () {
    this.getList();
  },
  methods: {
    onInput,
    /** 查询列表 */
    getList () {
      this.loading = true;
      getYaoList({
        ...this.queryParams,
        guaId: this.guaId
      }).then(
        (response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        guaId: this.guaId,
        yaoSort: null,
        yaoSex: null,
        ci: null,
        ciDesc: null,
        xiang: null,
        xiangDesc: null,
        jiXiong: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = '新增爻';
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      yaoDetail(row.yaoId).then((response) => {
        this.form = response.data;
        this.form.yaoSort = this.form.yaoSort.toString()
        this.open = true;
        this.title = '修改爻';
      });

    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.yaoId != undefined) {
            updateYao(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addYao(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const id = row.yaoId;
      this.$modal
        // .confirm('是否确认删除Rate id编号为"' + rateIds + '"的数据项？')
        .confirm(`是否确认删除数据项？`)
        .then(function () {
          return delYao(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => { });
    }
  },
};
</script>
